package com.eling.elcms.travel.service.impl;

import com.eling.elcms.basedoc.model.BusinessMessage;
import com.eling.elcms.basedoc.model.TargetType;
import com.eling.elcms.basedoc.model.TaskType;
import com.eling.elcms.basedoc.service.IBusinessMessageManager;
import com.eling.elcms.core.service.ServiceLocator;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.travel.dao.IAllianceSettlementDao;
import com.eling.elcms.travel.model.*;
import com.eling.elcms.travel.model.view.AllianceSetlGridView;
import com.eling.elcms.travel.model.view.AllianceSetlSaveView;
import com.eling.elcms.travel.model.view.SettlementsResult;
import com.eling.elcms.travel.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AllianceSettlementManagerImpl extends GenericManagerImpl<AllianceSettlement, Long> implements IAllianceSettlementManager{

	IAllianceSettlementDao allianceSettlementDao;
	
	@Autowired
	private IGroupReserveManager grManager;
	
	@Autowired
	private IAllianceConfirmManager acManager;
	
	@Autowired
	private IAllianceConfirmDetailManager acdManager;
	
	@Autowired
	private IAllianceSetlDetailManager  asdManager;
	
	@Autowired
	private ITravelContractManager contractM;
	@Autowired
 	private IBusinessMessageManager businessMessageManager;

	@Autowired
	public void setAllianceSettlementDao(IAllianceSettlementDao allianceSettlementDao) {
		this.dao = allianceSettlementDao;
		this.allianceSettlementDao = allianceSettlementDao;
	}

	/**
	 * 查询入住联盟结算列表数据
	 * @param cond
	 * @return
	 * @author zhoulin
	 */
	@Override
	public List<AllianceSetlGridView> querySetl(GroupReserve cond) {
		List<AllianceSetlGridView> returnList = new ArrayList<AllianceSetlGridView>();
		//查询所有已入住的预约
		List<GroupReserve> grList = grManager.query(cond);
		
		if(!grList.isEmpty()){
			List<Long> pkGRList = grList.stream().map(a->a.getPkGroupReserve()).collect(Collectors.toList());
			AllianceConfirm acCond = new AllianceConfirm();
			PropertyUtils.setProperty(acCond, "groupReserve.pkGroupReserveIn", pkGRList);
			
			//查询所有已确认的入住
			List<AllianceConfirm> acList = acManager.query(acCond);
			if(!acList.isEmpty()){
				List<Long> pkACList = acList.stream().map(b->b.getPkAllianceConfirm()).collect(Collectors.toList());
				AllianceConfirmDetail acdCond = new AllianceConfirmDetail();
				PropertyUtils.setProperty(acdCond, "allianceConfirm.pkAllianceConfirmIn", pkACList);
				//查询所有入住确认时的报名人数
				List<AllianceConfirmDetail> acdList = acdManager.query(acdCond);
				
				AllianceSettlement asCond = new AllianceSettlement();
				PropertyUtils.setProperty(asCond, "allianceConfirm.pkAllianceConfirmIn", pkACList);
				//查询所有联盟结算
				List<AllianceSettlement> asList = allianceSettlementDao.query(asCond);
				for(AllianceConfirm ac : acList){
					AllianceSetlGridView asgv = new AllianceSetlGridView();
					asgv.setAllianceConfirm(ac);
					Integer signUpNumber = 0;
					//加入结算数据
					for(AllianceSettlement as : asList){
						if(as.getAllianceConfirm().getPkAllianceConfirm().equals(ac.getPkAllianceConfirm())){
							asgv.setAllianceSettlement(as);
							break;
						}
					}
					//计算报名人数
					for(AllianceConfirmDetail acd : acdList){
						if(acd.getAllianceConfirm().getPkAllianceConfirm().equals(ac.getPkAllianceConfirm())){
							signUpNumber += 1;
						}
					}
					asgv.setSignUpNumber(signUpNumber);
					returnList.add(asgv);
				}
			}
		}
		
		return returnList;
	}

	/**
	 * 入住联盟结算保存
	 * @param assView
	 * @return
	 * @author zhoulin
	 */
	@Override
	public Msg saveSetl(AllianceSetlSaveView assView) {
		assView.getAllianceSettlement().setConfirmStatus(AllianceSettlement.ConfirmStatus.NoConfirm);
		AllianceSettlement asCond = super.save(assView.getAllianceSettlement());
		
		List<AllianceSetlDetail> asdList = assView.getAsdList();
		if(!asdList.isEmpty()){
			for(AllianceSetlDetail asd : asdList){
				asd.setAllianceSettlement(asCond);
			}
			asdManager.save(asdList);
		}
		businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.AllianceSetlList,999L));

		return new Msg("保存成功");
	}
	
	@Override
	public SettlementsResult getAllianceRuleMoney(SignUpRegister signUp, RuleType type,GroupReserve groupReserve) {
		TravelContract contract = signUp.getTravelContract();

		// 入住人员的权证信息不为空，则是内部会员，内部会员入住联盟机构
		if (contract != null){
				// 抵扣范围匹配时计算抵扣份数及金额
				SettlementsRule rule = contractM.getContractSetlRuleByType(signUp.getTravelContract(), type);
				if (rule != null){
					// 获取对应的规则实现类
					ISettlementsRule ruleImpl = ServiceLocator.getService(rule.getServiceImpl());
					// 返回合同对应规则的本次抵扣、消费卷、其他的金额
					return ruleImpl.calculate(null,contract, groupReserve.getCheckInDate(),groupReserve.getCheckOutDate());
				}else{
					return new SettlementsResult();
				}
		}else{
			return new SettlementsResult();
		}
	}
}
